package com.example.goodpet.my.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.goodpet.R;
import com.example.goodpet.base.BaseFragmentForView;
import com.example.goodpet.common.LoadingPage;
import com.example.goodpet.my.adapter.MyPetCollectAdapter;
import com.example.goodpet.bean.ResponseBody;
import com.example.goodpet.bean.UserPetInfo;
import com.example.goodpet.common.EndlessRecyclerOnScrollListener;
import com.example.goodpet.common.LoadMoreWrapper;
import com.example.goodpet.service.UserService;
import com.example.goodpet.utils.MyApplication;
import com.example.goodpet.utils.RetrofitSingleton;
import com.example.goodpet.utils.UIUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MyPetCollectFragment extends BaseFragmentForView {
    private RecyclerView recyclerView;
    private List<UserPetInfo> data;

    private Retrofit retrofitNetConfig;
    private UserService userService;
    @Override
    protected void LoadPage() {
        loadingPage.setStateCurrent(LoadingPage.STATE_loading);
        userService.getPetCollection(MyApplication.user.getId(),1).enqueue(new Callback<ResponseBody<List<UserPetInfo>>>() {
            @Override
            public void onResponse(Call<ResponseBody<List<UserPetInfo>>> call, Response<ResponseBody<List<UserPetInfo>>> response) {
                ResponseBody<List<UserPetInfo>> body = response.body();
                if (body!=null) {
                    loadingPage.setStateCurrent(LoadingPage.STATE_SUCCESS);
                    int code = body.getCode();
                    if (code == 200) {
                        //成功
                        data = body.getData();
                        MyPetCollectAdapter myPetCollectAdapter = new MyPetCollectAdapter(data, getActivity());
                        LoadMoreWrapper loadMoreWrapper = new LoadMoreWrapper(myPetCollectAdapter);
                        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2));
                        recyclerView.setAdapter(loadMoreWrapper);
                        recyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener() {
                            @Override
                            public void onLoadMore() {
                                loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING);
                                userService.getPetCollection(MyApplication.user.getId(), ++loadMoreWrapper.page).enqueue(new Callback<ResponseBody<List<UserPetInfo>>>() {
                                    @Override
                                    public void onResponse(Call<ResponseBody<List<UserPetInfo>>> call, Response<ResponseBody<List<UserPetInfo>>> response) {
                                        ResponseBody<List<UserPetInfo>> body1 = response.body();
                                        //Log.d(TAG, "onResponse: "+ body);
                                        if (body1.getCode() == 200) {
                                            List<UserPetInfo> data1 = body1.getData();
                                            data.addAll(data1);
                                            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_COMPLETE);
                                        } else if (body1.getCode() == 404) {
                                            loadMoreWrapper.setLoadState(loadMoreWrapper.LOADING_END);
                                        }

                                    }

                                    @Override
                                    public void onFailure(Call<ResponseBody<List<UserPetInfo>>> call, Throwable t) {
                                        UIUtils.toast("连接服务器失败", false);
                                    }
                                });
                            }
                        });
                    } else if (code == 404) {
                        loadingPage.setStateCurrent(LoadingPage.STATE_EMPTY);
                    }
                }
            }

            @Override
            public void onFailure(Call<ResponseBody<List<UserPetInfo>>> call, Throwable t) {
                loadingPage.setStateCurrent(LoadingPage.STATE_ERROR);
            }
        });
    }

    /**
     * 从服务端获取数据
     */
    public void initData(){
        retrofitNetConfig = RetrofitSingleton.getRetrofitNetConfig();
        userService = retrofitNetConfig.create(UserService.class);
        data=new ArrayList<>();
    }

    @Override
    protected void initView(View inflate) {
        super.initView(inflate);
        recyclerView=inflate.findViewById(R.id.recyclerView);
    }

    @Override
    public void initListener() {
        super.initListener();
    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_user_info_rv;
    }
}
